package com.dangdang.dao;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.dangdang.domain.Book;
import com.dangdang.domain.Category;
import com.dangdang.util.HbnUtil;

public class BookDao {

	/**
	 * @insertBook
	 * @param product
	 */
	public void InsertBook(Book book) {
		Session s = HbnUtil.getSession();
		try {
			s.beginTransaction();
			s.save(book);
			s.getTransaction().commit();
		} catch (HibernateException e) {
			s.getTransaction().rollback();
			e.printStackTrace();
		}
		s.close();
	}

	/**
	 * @getBookList
	 * @return bookList
	 */
	public List<Book> getBook(Integer categoryId, String name, String author,
			String publisher) {
		Session s = HbnUtil.getSession();
		
		 /** 在join fetch后面跟了别名就是查的集合中的要关联的那张表 */
		//String hql = "from Book b join fetch b.category cat " +
		//			  "where id=? and b.name=?";
			Criteria c = s.createCriteria(Book.class);
			/** c.createAlias("原来的名字", "别名"); 加了别名,就等于是加了 join tetch ... */
			c.createAlias("category", "cat");
			if ( categoryId != null && categoryId >= 0 ) {
				c.add(Restrictions.eq("cat.id", categoryId));
			}
			if (!name.equals("") && name !=null) {
				/** 一个约束条件,这里相当于from */
				c.add(Restrictions.like("name", name.trim())); 
			}
			if (!author.equals("") && author !=null) {
				c.add(Restrictions.like("author", author.trim()));
			}
			if (!publisher.equals("") && publisher !=null) {
				c.add(Restrictions.like("publisher", publisher.trim()));
			}
			
			/** 如果不添加上面的条件，就直接作form book */
			List<Book>  bookList = c.list();
			Set<Book> set = new HashSet<Book>();
			set.addAll(bookList);
			bookList.clear();
			bookList.addAll(set);
			Collections.sort(bookList,new Comparator<Book>(){
				public int compare(Book book1, Book book2) {
					return book1.getId() - book2.getId();
				}});
			s.close();
		return bookList;
	}

	public static void main(String[] args) {
		BookDao bd = new BookDao();
		List<Book> list = bd.getBook(1, "Java入门", "科学家", "地球出版社");
		for(Book l : list ){
			System.out.println(l.toString());
		}
	}
	
	public List<Category> getCategory() {
		Session s = HbnUtil.getSession();
			Query query = s.createQuery("from Category");
			List<Category> categoryList = query.list();
		
		Map<Integer, Category> map = new HashMap<Integer, Category>();
		
		/** 将得到的List集合放在Map集合中， */
		for(Category c : categoryList){
			map.put(c.getId(),c);
		}
		for(Category c : categoryList){
			setCatValue(map, c);
		}
		Collections.sort(categoryList,new Comparator<Category>(){
			public int compare(Category c1,Category c2){
				return c1.getCatValue().compareTo(c2.getCatValue());
			}
		});
			
		s.close();
		return categoryList;

	}

	/**
	 * @setCatValue
	 * @param map
	 * @param cat
	 * @return
	 */
	public String setCatValue(Map<Integer, Category> map, Category cat) {
		if (cat.getCatValue() == null) { 		// 如果这个排序标签为空
			if (cat.getParentId() == 0) { 		// 如果这个父ID为0
				/** 将这个类别的排序标签设置为它的父ID */
				cat.setCatValue(String.valueOf(cat.getId()));
			} else {
				/** 将 */
				cat.setCatValue(setCatValue(map, map.get(cat.getParentId()))
						+ "|" + cat.getId());
			}
		}
		return cat.getCatValue();
	}

	/**
	 * @update a Book
	 * @param Book
	 */
	public void updateBook(Book book) {
		Session s = HbnUtil.getSession();
		try {
			s.beginTransaction();

			s.getTransaction().commit();

		} catch (HibernateException e) {
			e.printStackTrace();
			s.getTransaction().rollback();
		}
	}
}
